package com.huawei.updatesdk.a.b.b;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.updatesdk.service.b.c;
import com.huawei.updatesdk.service.c.g;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

public class b extends AsyncTask<Void, Void, Void> {
    private HttpURLConnection a;
    private boolean b = false;
    private a c;
    private Handler d = new a();

    static class a extends Handler {
        private a() {
        }

        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.obj instanceof a) {
                a aVar = (a) message.obj;
                Intent intent = new Intent();
                com.huawei.updatesdk.a.b.c.b a = com.huawei.updatesdk.a.b.c.b.a(intent);
                switch (message.what) {
                    case 2:
                        intent.putExtra("download_apk_size", aVar.b());
                        intent.putExtra("download_apk_already", aVar.a());
                        c.a().b(a);
                        return;
                    case 3:
                    case 5:
                        break;
                    case 4:
                        g.a(b.d() + "/appmarket.apk", "com.huawei.appmarket");
                        break;
                    default:
                        return;
                }
                Bundle bundle = new Bundle();
                bundle.putInt("download_status_param", message.what);
                intent.putExtras(bundle);
                c.a().a(a);
            }
        }
    }

    public b(a aVar) {
        this.c = aVar;
    }

    private void a(int i, int i2, int i3) {
        a aVar = new a();
        aVar.a(i);
        aVar.b(i2);
        if (this.d != null) {
            this.d.sendMessage(this.d.obtainMessage(i3, aVar));
        }
    }

    private void a(a aVar, String str) {
        FileNotFoundException e;
        Throwable th;
        Exception e2;
        if (aVar != null && !TextUtils.isEmpty(aVar.c())) {
            String str2 = str + "/appmarket.apk";
            FileOutputStream fileOutputStream;
            try {
                fileOutputStream = new FileOutputStream(new File(str2));
                try {
                    if (!a(aVar, (OutputStream) fileOutputStream, str2)) {
                        com.huawei.updatesdk.a.a.c.c.a(new File(d()));
                    }
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e3) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "Close FileOutputStream failed!");
                        }
                    }
                } catch (FileNotFoundException e4) {
                    e = e4;
                    try {
                        com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "download market package FileNotFoundException error:" + e.toString());
                        if (fileOutputStream != null) {
                            try {
                                fileOutputStream.close();
                            } catch (IOException e5) {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "Close FileOutputStream failed!");
                            }
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        if (fileOutputStream != null) {
                            try {
                                fileOutputStream.close();
                            } catch (IOException e6) {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "Close FileOutputStream failed!");
                            }
                        }
                        throw th;
                    }
                } catch (Exception e7) {
                    e2 = e7;
                    com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "download market package Exception error:" + e2.toString());
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e8) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "Close FileOutputStream failed!");
                        }
                    }
                }
            } catch (FileNotFoundException e9) {
                e = e9;
                fileOutputStream = null;
                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "download market package FileNotFoundException error:" + e.toString());
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (Exception e10) {
                e2 = e10;
                fileOutputStream = null;
                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "download market package Exception error:" + e2.toString());
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = null;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                throw th;
            }
        }
    }

    private void a(InputStream inputStream, OutputStream outputStream) {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e2) {
                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e2.toString());
            }
        }
    }

    private boolean a(a aVar, OutputStream outputStream, String str) {
        int b;
        OutputStream bufferedOutputStream;
        Throwable th;
        InputStream inputStream;
        Exception e;
        OutputStream outputStream2;
        int i;
        boolean z;
        InputStream inputStream2;
        int i2;
        int i3;
        boolean z2;
        InputStream inputStream3 = null;
        OutputStream outputStream3 = null;
        int i4 = 0;
        boolean z3 = false;
        try {
            this.a = c.a().a(aVar.c());
            if (this.a == null) {
                if (this.b) {
                    a(0, -1, 3);
                } else {
                    a(0, -1, 5);
                }
                a(null, null);
                e();
                return false;
            }
            this.a.connect();
            int responseCode = this.a.getResponseCode();
            if (200 == responseCode || 206 == responseCode) {
                b = aVar.b();
                try {
                    long j;
                    InputStream bufferedInputStream = new BufferedInputStream(this.a.getInputStream(), 8192);
                    try {
                        bufferedOutputStream = new BufferedOutputStream(outputStream, 8192);
                        j = 0;
                    } catch (Throwable th2) {
                        th = th2;
                        bufferedOutputStream = null;
                        inputStream = bufferedInputStream;
                        try {
                            a(inputStream, bufferedOutputStream);
                            inputStream3 = null;
                            outputStream3 = null;
                            try {
                                throw th;
                            } catch (Exception e2) {
                                e = e2;
                                outputStream2 = null;
                                i = b;
                                z = false;
                                inputStream2 = null;
                                i2 = 0;
                            } catch (Throwable th3) {
                                th = th3;
                                if (!z3) {
                                    if (this.b) {
                                        a(i4, b, 3);
                                    } else {
                                        a(i4, b, 5);
                                    }
                                }
                                a(inputStream3, outputStream3);
                                e();
                                throw th;
                            }
                        } catch (Exception e3) {
                            e = e3;
                            outputStream2 = bufferedOutputStream;
                            i2 = 0;
                            z = false;
                            inputStream2 = inputStream;
                            i = b;
                            try {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                                if (!z) {
                                    if (this.b) {
                                        a(i2, i, 3);
                                    } else {
                                        a(i2, i, 5);
                                    }
                                }
                                a(inputStream2, outputStream2);
                                e();
                                return false;
                            } catch (Throwable th4) {
                                th = th4;
                                b = i;
                                outputStream3 = outputStream2;
                                boolean z4 = z;
                                i4 = i2;
                                inputStream3 = inputStream2;
                                z3 = z4;
                                if (z3) {
                                    if (this.b) {
                                        a(i4, b, 5);
                                    } else {
                                        a(i4, b, 3);
                                    }
                                }
                                a(inputStream3, outputStream3);
                                e();
                                throw th;
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            OutputStream outputStream4 = bufferedOutputStream;
                            inputStream3 = inputStream;
                            outputStream3 = outputStream4;
                            if (z3) {
                                if (this.b) {
                                    a(i4, b, 3);
                                } else {
                                    a(i4, b, 5);
                                }
                            }
                            a(inputStream3, outputStream3);
                            e();
                            throw th;
                        }
                    }
                    try {
                        byte[] bArr = new byte[8192];
                        while (true) {
                            i = bufferedInputStream.read(bArr);
                            if (i != -1) {
                                bufferedOutputStream.write(bArr, 0, i);
                                i4 += i;
                                if (Math.abs(System.currentTimeMillis() - j) > 1000) {
                                    j = System.currentTimeMillis();
                                    if (i4 != b) {
                                        a(i4, b, 2);
                                    }
                                }
                            } else {
                                try {
                                    break;
                                } catch (Exception e4) {
                                    e = e4;
                                    i = b;
                                    i3 = i4;
                                    z = false;
                                    inputStream2 = bufferedInputStream;
                                    outputStream2 = bufferedOutputStream;
                                    i2 = i3;
                                    com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                                    if (z) {
                                        if (this.b) {
                                            a(i2, i, 3);
                                        } else {
                                            a(i2, i, 5);
                                        }
                                    }
                                    a(inputStream2, outputStream2);
                                    e();
                                    return false;
                                } catch (Throwable th6) {
                                    th = th6;
                                    outputStream3 = bufferedOutputStream;
                                    inputStream3 = bufferedInputStream;
                                    if (z3) {
                                        if (this.b) {
                                            a(i4, b, 3);
                                        } else {
                                            a(i4, b, 5);
                                        }
                                    }
                                    a(inputStream3, outputStream3);
                                    e();
                                    throw th;
                                }
                            }
                        }
                        a(bufferedInputStream, bufferedOutputStream);
                        outputStream3 = null;
                        if (i4 == b && b > 0) {
                            try {
                                if (!TextUtils.isEmpty(aVar.d())) {
                                    if (aVar.d().equalsIgnoreCase(com.huawei.updatesdk.a.a.c.c.a(str, AaidIdConstant.SIGNATURE_SHA256))) {
                                        z2 = true;
                                        if (z2) {
                                            try {
                                                a(i4, b, 4);
                                            } catch (Exception e5) {
                                                inputStream2 = null;
                                                outputStream2 = null;
                                                i = b;
                                                i3 = i4;
                                                z = z2;
                                                e = e5;
                                                i2 = i3;
                                                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                                                if (z) {
                                                    if (this.b) {
                                                        a(i2, i, 3);
                                                    } else {
                                                        a(i2, i, 5);
                                                    }
                                                }
                                                a(inputStream2, outputStream2);
                                                e();
                                                return false;
                                            } catch (Throwable th7) {
                                                z3 = z2;
                                                th = th7;
                                                inputStream3 = null;
                                                if (z3) {
                                                    if (this.b) {
                                                        a(i4, b, 3);
                                                    } else {
                                                        a(i4, b, 5);
                                                    }
                                                }
                                                a(inputStream3, outputStream3);
                                                e();
                                                throw th;
                                            }
                                        }
                                        if (!z2) {
                                            if (this.b) {
                                                a(i4, b, 5);
                                            } else {
                                                a(i4, b, 3);
                                            }
                                        }
                                        a(null, null);
                                        e();
                                        return z2;
                                    }
                                }
                            } catch (Exception e6) {
                                e = e6;
                                i2 = i4;
                                z = false;
                                inputStream2 = null;
                                outputStream2 = null;
                                i = b;
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                                if (z) {
                                    if (this.b) {
                                        a(i2, i, 5);
                                    } else {
                                        a(i2, i, 3);
                                    }
                                }
                                a(inputStream2, outputStream2);
                                e();
                                return false;
                            } catch (Throwable th8) {
                                th = th8;
                                inputStream3 = null;
                                if (z3) {
                                    if (this.b) {
                                        a(i4, b, 5);
                                    } else {
                                        a(i4, b, 3);
                                    }
                                }
                                a(inputStream3, outputStream3);
                                e();
                                throw th;
                            }
                        }
                        z2 = false;
                        if (z2) {
                            a(i4, b, 4);
                        }
                        if (z2) {
                            if (this.b) {
                                a(i4, b, 5);
                            } else {
                                a(i4, b, 3);
                            }
                        }
                        a(null, null);
                        e();
                        return z2;
                    } catch (Throwable th9) {
                        th = th9;
                        inputStream = bufferedInputStream;
                        a(inputStream, bufferedOutputStream);
                        inputStream3 = null;
                        outputStream3 = null;
                        throw th;
                    }
                } catch (Throwable th10) {
                    th = th10;
                    inputStream = null;
                    bufferedOutputStream = null;
                    a(inputStream, bufferedOutputStream);
                    inputStream3 = null;
                    outputStream3 = null;
                    throw th;
                }
            } else {
                if (this.b) {
                    a(0, -1, 3);
                } else {
                    a(0, -1, 5);
                }
                a(null, null);
                e();
                return false;
            }
        } catch (Exception e7) {
            e = e7;
            inputStream2 = null;
            i2 = 0;
            z = false;
            i = -1;
            outputStream2 = null;
            com.huawei.updatesdk.a.a.b.a.a.a.d("DownloadMarketTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
            if (z) {
                if (this.b) {
                    a(i2, i, 5);
                } else {
                    a(i2, i, 3);
                }
            }
            a(inputStream2, outputStream2);
            e();
            return false;
        } catch (Throwable th11) {
            th = th11;
            b = -1;
            if (z3) {
                if (this.b) {
                    a(i4, b, 5);
                } else {
                    a(i4, b, 3);
                }
            }
            a(inputStream3, outputStream3);
            e();
            throw th;
        }
    }

    private static String d() {
        Object c = com.huawei.updatesdk.a.b.a.a.a().c();
        if (TextUtils.isEmpty(c)) {
            return "";
        }
        String str = c + "/updatesdk";
        File file = new File(str);
        return (file.exists() || file.mkdirs()) ? str : "";
    }

    private void e() {
        if (this.a != null) {
            this.a.disconnect();
        }
    }

    protected Void a(Void... voidArr) {
        a(this.c, d());
        return null;
    }

    public void a() {
        if (this.a != null) {
            this.a.disconnect();
            this.b = true;
        }
        com.huawei.updatesdk.a.a.c.c.a(new File(d()));
    }

    public void b() {
        this.d = null;
    }

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return a((Void[]) objArr);
    }
}
